require 'yaml'
require 'digest/md5'

class Database
  
  attr_accessor :users, :galleries, :images, :comments, :votes
  
  def initialize
    @users = Array.new
    @galleries = Array.new
    @images = Array.new
    @comments = Array.new
    @votes = Array.new

    output = File.new("#{DATA_PATH}/users.yaml",'r')
    @users = YAML.load(output.read)
    output.close
    output = File.new("#{DATA_PATH}/galleries.yaml",'r')
    @galleries = YAML.load(output.read)
    output.close
    output = File.new("#{DATA_PATH}/images.yaml",'r')
    @images = YAML.load(output.read)
    output.close
    output = File.new("#{DATA_PATH}/comments.yaml",'r')
    @comments = YAML.load(output.read)
    output.close
    output = File.new("#{DATA_PATH}/votes.yaml",'r')
    @votes = YAML.load(output.read)
    output.close
  end

  def insert (object)
    add_user(object) if object.class == User
    @galleries.push object if object.class == Gallery
    @images.push object if object.class == Image
    @comments.push object if object.class == Comment
    @votes.push object if object.class == Vote
  end

  def add_user(user)
    return -1 if !valid_email(user.email)
    @users.each {|usr|
      return -1 if usr.username == user.username
    }
    @users.push user
    return 1
  end

  def add_vote(image, user, grade)
    @votes.each {|vote|
      return if vote.user == user && vote.image == image
    }
    @votes.push Vote.new(image, user, grade)
  end

  def print_all_data
    puts "Comments:"
    @users.each {|user|
      puts user.id
    }

    puts "Galleries:"
    @galleries.each {|gallery|
      puts gallery.id
    }

    puts "Images:"
    @images.each {|image|
      puts image.id
    }

    puts "Comments:"
    @comments.each {|comment|
      puts comment.id
    }

    puts "Votes:"
    @votes.each {|vote|
      puts vote.id
    }
  end

  def save_all
    output = File.new("#{DATA_PATH}/users.yaml",'w')
    output.puts YAML.dump(@users)
    output.close
    output = File.new("#{DATA_PATH}/galleries.yaml",'w')
    output.puts YAML.dump(@galleries)
    output.close
    output = File.new("#{DATA_PATH}/images.yaml",'w')
    output.puts YAML.dump(@images)
    output.close
    output = File.new("#{DATA_PATH}/comments.yaml",'w')
    output.puts YAML.dump(@comments)
    output.close
    output = File.new("#{DATA_PATH}/votes.yaml",'w')
    output.puts YAML.dump(@votes)
    output.close
  end

  def user_authenticate(username, password)
    password = Digest::MD5.hexdigest(password)
    @users.each {|user|
      return user if user.username == username && user.password == password
    }
    return -1
  end

  def get_by_id (object, id)

    @users.each {|obj|
      if obj.id == id then
        return obj
      end
    } if object == "user"

    @galleries.each {|obj|
      if obj.id == id then
        return obj
      end
    } if object == "gallery"

    @images.each {|obj|
      if obj.id == id then
        return obj
      end
    } if object == "image"

    @votes.each {|obj|
      if obj.id == id then
        return obj
      end
    } if object == "vote"

    @comments.each {|obj|
      if obj.id == id then
        return obj
      end
    } if object == "comment"
    
  end

end
